﻿using System;
using System.Collections.Generic;
using System.Linq;
using EmptyWallGallery.Core.Utilities;

namespace EmptyWallGallery.Core.BLL
{
    public class AttachFilesListView
    {
        public string FileId { get; set; }
        public string FileName { get; set; }
        public string FilePath { get; set; }
        public int FileSize { get; set; }
        public string FileType { get; set; }
        public int TotalDownload { get; set; }
        public int ArticleId { get; set; }
        public int ReportId { get; set; }
        public DateTime CreateDate { get; set; }
        public int Type { get; set; }
    }

    public class AttachFiles
    {
        public static AttachFiles Instance
        {
            get { return new AttachFiles(); }
        }

        public static AttachFilesListView LoadDataFromListItem(AttachFile item)
        {
            var objFileAttach = new AttachFilesListView
                                 {
                                     FileId = item.FileId.ToString(),
                                     FileName = item.FileName,
                                     FilePath = item.FilePath,
                                     FileSize = Convert.ToInt32(item.FileSize),
                                     FileType = item.FileType,
                                     ArticleId = item.ArticleId,
                                     ReportId = Convert.ToInt32(item.ReportId),
                                     CreateDate = Convert.ToDateTime(item.CreateDate)
                                 };

            return objFileAttach;
        }

        /// <summary>
        /// Create File Attach
        /// </summary>
        /// <param name="name">File Name</param>
        /// <param name="path">File Path</param>
        /// <param name="fileSize">File Size</param>
        /// <param name="fileType">File Type</param>
        /// <param name="articleId">Article Id</param>
        /// <param name="reportId">Report ID</param>
        /// <returns></returns>
        public bool Create(string name, string path, int fileSize, string fileType, int articleId, int reportId, int type)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objFileAttach = new AttachFile
                                            {
                                                FileId = Guid.NewGuid(),
                                                FileName = name,
                                                FilePath = path,
                                                FileSize = fileSize,
                                                FileType = fileType,
                                                TotalDownload = 0,
                                                ArticleId = articleId,
                                                ReportId = reportId,
                                                CreateDate = DateTime.Now
                                            };

                    dc.AttachFiles.InsertOnSubmit(objFileAttach);
                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Update File Attach
        /// </summary>
        /// <param name="name">File Name</param>
        /// <param name="path">File Path</param>
        /// <param name="fileSize">File Size</param>
        /// <param name="fileType">File Type</param>
        /// <param name="articleId">Article Id</param>
        /// <param name="fileId">File Id</param>
        /// <returns></returns>
        public bool Update(string name, string path, int fileSize, string fileType, int articleId, string fileId, int reportId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objFileAttach = (dc.AttachFiles.Where(p => p.FileId == new Guid(fileId))).Single();
                    objFileAttach.FileName = name;
                    objFileAttach.FilePath = path;
                    objFileAttach.FileSize = fileSize;
                    objFileAttach.FileType = fileType;
                    objFileAttach.ArticleId = articleId;
                    objFileAttach.ReportId = reportId;

                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Update Number Download
        /// </summary>
        /// <param name="fileId">File Id</param>
        /// <returns></returns>
        public bool UpdateDownload(string fileId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objFileAttach = (dc.AttachFiles.Where(p => p.FileId == new Guid(fileId))).Single();
                    objFileAttach.TotalDownload = objFileAttach.TotalDownload + 1;

                    dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Delete File Attach
        /// </summary>
        /// <param name="fileId">File Id</param>
        /// <returns></returns>
        public bool Delete(string fileId)
        {
            try
            {
                using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
                {
                    var objFileAttach = (dc.AttachFiles.Where(p => p.FileId == new Guid(fileId))).Single();

                    var strPath = Configs.UserFolder + objFileAttach.FilePath.Replace("/", @"\");
                    if (Common.Instance.DeleteFile(strPath))
                    {
                        dc.AttachFiles.DeleteOnSubmit(objFileAttach);
                        dc.SubmitChanges();
                        return true;
                    }
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Get File By Article
        /// </summary>
        /// <param name="articleId">Article Id</param>
        /// <returns></returns>
        public List<AttachFilesListView> GetByArticle(int articleId)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var query = from a in dc.AttachFiles
                            where a.ArticleId == articleId
                            select new AttachFilesListView
                            {
                                FileId = a.FileId.ToString(),
                                FileName = a.FileName,
                                FilePath = a.FilePath,
                                FileSize = Convert.ToInt32(a.FileSize),
                                FileType = a.FileType,
                                TotalDownload = Convert.ToInt32(a.TotalDownload),
                                ArticleId = a.ArticleId,
                                ReportId = Convert.ToInt32(a.ReportId),
                                CreateDate = Convert.ToDateTime(a.CreateDate)
                            };

                return query.ToList();
            }
        }

        public List<AttachFilesListView> GetByReport(int reportId)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var query = from a in dc.AttachFiles
                            where a.ReportId == reportId
                            select new AttachFilesListView
                            {
                                FileId = a.FileId.ToString(),
                                FileName = a.FileName,
                                FilePath = a.FilePath,
                                FileSize = Convert.ToInt32(a.FileSize),
                                FileType = a.FileType,
                                TotalDownload = Convert.ToInt32(a.TotalDownload),
                                ArticleId = a.ArticleId,
                                ReportId = Convert.ToInt32(a.ReportId),
                                CreateDate = Convert.ToDateTime(a.CreateDate)
                            };

                return query.ToList();
            }
        }

        public AttachFilesListView GetByArticleEx(int articleId)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var query = (from a in dc.AttachFiles
                            where a.ArticleId == articleId select a).FirstOrDefault();

                return LoadDataFromListItem(query);
            }
        }

        /// <summary>
        /// Get By Id
        /// </summary>
        /// <param name="fileId">File Id</param>
        /// <returns></returns>
        public AttachFilesListView GetById(string fileId)
        {
            using (var dc = new EmptyWallGalleryDataContext(Configs.ConnectionString))
            {
                var query = (from a in dc.AttachFiles
                             where a.FileId == new Guid(fileId)
                             select a).SingleOrDefault();

                return LoadDataFromListItem(query);
            }
        }        
    }
}
